package com.huluobo.a2204kotlinshopping.splash

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.text.TextUtils
import com.blankj.utilcode.util.SPUtils
import com.huluobo.a2204kotlinshopping.MainActivity
import com.huluobo.a2204kotlinshopping.R
import com.huluobo.a2204kotlinshopping.databinding.ActivitySplashBinding
import com.huluobo.a2204kotlinshopping.login.LoginActivity
import com.huluobo.base.net.Const
import com.huluobo.base.view.BaseActivity

class SplashActivity : BaseActivity<ActivitySplashBinding>() {

    private val countDownTimer = object : CountDownTimer(2000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            dataBinding.splashCount.text = "${(millisUntilFinished / 1000).toInt()}秒"
        }

        override fun onFinish() {
            val token = SPUtils.getInstance().getString(Const.PARAM_TOKEN)
            if (TextUtils.isEmpty(token)) {
                val intent = Intent(this@SplashActivity, LoginActivity::class.java)
                startActivity(intent)
            } else {
                val intent = Intent(this@SplashActivity, MainActivity::class.java)
                startActivity(intent)
            }
            this@SplashActivity.finish()
        }
    }

    override fun initData() {
    }

    override fun initView() {
        countDownTimer.start()
    }

    override fun getLayoutId(): Int = R.layout.activity_splash
}